window.onload = function() {
	seajs.use(['jquery-1.8.2.js', 'iscroll_module.js', 'mdialog_module.js', 'util_module.js'], function(Z, I, M, U) {
		U.fillHead();
		U.fillFooter();
		var id = U.getParam('id');
		var curPage = 1;
		var pageSize = 5;
		var menus = new Array();
		var user = U.checkLogin();
		var _isPoverty;
		var specialId = U.getParam("specialId");		//specialId-2跳转到专项帮扶对象 不传跳转到精准贫困户
		if(specialId == null){
			specialId = 0;
		}
		initPage();

		function initPage() { //初始化页面
			U.loadOpt(false);
			$("#content>div").show();
			$(".family_member").addClass("divColor");
			//$('.team_info').hide();
			initEvent();
		}

		function initEvent() {
			U.loadOpt(false);
			//点击申请帮扶
			var personVO="";
			U.ajaxget('/person/detailByUid/' + user.info.id, {}, function(json) {
				if (json.success) {
					personVO = json.models.personVO;
					//cardType = povertyObjVO.cardType == 1 ? '身份证' : '其它类型';
					/*if (povertyObjVO.valiState == 1) {//待审核
						show="审核中";
					}
					if (povertyObjVO.valiState == 2) {//审核未通过
						show="审核未通过";
					}

					if (povertyObjVO.valiState == 3) {//审核通过
						show="审核通过";
					}*/
				}
			});
			//点击申请帮扶按钮
			$(".apply_teamHelp").click(function(){
				if(_isPoverty == 1){				//已结对
					M.toast(false, "帮扶对象已结对");
					U.loadOpt(false);
					return;
				}
				else {								//未结对
					if(user.state != 1){
						M.toast(false, "请先登录");
						U.loadOpt(false);
						U.loadLogin(true);
					}
					else{
						if(user.info.typ == 0) {
							M.toast(false, "您还不是帮扶队员，请前往申请!");
							//跳转到申请帮扶页面
							location.href = "apply.html";
							return false;
						} else if(user.info.typ == 3){
							M.toast(false, "您是贫困户，无法申请帮扶!");
							return false;
						}
						else if(user.info.typ == 4){
							if(personVO.status != 2){
								M.toast(false, "您的资料还未审核通过，无法申请帮扶!");
								return false;
							}
							window.location.href="help_submit.html?provinceId="+id;
						}
					}
				}
			});
			//点击我要捐助
			$(".donate").click(function(){
				window.open("help_entrance.html?poor_id=" + id);
				/*if(user.state != 1){ 	//未登录状态
					window.open("help_entrance.html?poor_id=" + id);
				}
				else{					//登录状态
					window.open("help_entrance.html?poor_id=" + id);
				}*/
			});
			//切换菜单
			menus[0] = $('.poor_list')[0];
			menus[1] = $('.record_list')[0];
			menus[2] = $('.live_list')[0];
			$('.menu div').click(function() {
				var index = $(this).index();
				$(menus).css('display', 'none');
				$(menus[index]).show();
				if(index==1){				//被帮扶记录
					$(".family_member").removeClass("divColor");
					$(".live_record").removeClass("divColor");
					$(".help_record").addClass("divColor");
					get_helpRecord();
				}else if(index==2){			//真实记录
					$(".help_record").removeClass("divColor");
					$(".family_member").removeClass("divColor");
					$(".live_record").addClass("divColor");
				}
				else {
					$(".help_record").removeClass("divColor");
					$(".live_record").removeClass("divColor");
					$(".family_member").addClass("divColor");
				}
			});
			//页面信息
			U.ajax('/poverty/detail/' + id, {}, function(json) {
				if(json.success) {
					var result = json.models.povertyObjVO;
					_isPoverty = result.isPoverty;
					if(result.state == 1){     //1是已脱贫 已脱贫 底部按钮隐藏,0是未脱贫
						$(".isPoor").css("display","block");
					}
					else {
						$(".isPoor").css("display","none");
						if(specialId == -2){
							$(".btnList").css("display","none");
							$(".oneBtn").css("display","block");
						}else {
							$(".oneBtn").css("display","none");
							$(".btnList").css("display","block");
						}
					}
					$('.name').text(result.name);							//户主姓名
					if(user.state == 0){
						$('.phone').text(U.formPhone(result.connect));			//手机号
					}
					else {
						$('.phone').text(result.connect);			//手机号
					}
					$('.address').text(result.area);						//地址
					$('.aye').text(result.yearAmount);						//平均年收入
					//是否低保：0-否，1-是
					if(result.low == 0){
						$('.isAllowances').text("否");						//是否低保
					}
					else {
						$('.isAllowances').text("是");						//是否低保
					}
					/*$('.member_amount').text(result.personCount);*/
					$('.requests').text(result.requirement);				//对帮扶的要求
					//$('.reason').text(result.reason);						//致贫原因
					$('.poor_status').text(result.currentReason);			//贫困现状
					$('.remark').text(result.remark);						//备注

				} else {
					M.toast(false, json.errrorMsg)
				}
				var povertyObjPersonList = result.povertyObjPersonList;
				$(".live_list_content").html(result.intro);					//真实记录
				var _html = "";
				$('.poor_list tbody').html('');
				$(povertyObjPersonList).each(function() {
					if(this.connect == null) {
						this.connect = '';
					}
					var sex = this.sex == 1 ? '女' : '男';
					_html = '<tr><td>' + this.relation + '</td><td>'+this.name.substring(0,1)+'***'+'</td><td>' + this.age + '</td><td>' + sex + '</td><td>' + this.remark + '</td></tr>';
					$('.poor_list tbody').append(_html);
				});
			});
		}

		//被帮扶记录
		function get_helpRecord(){
			$('.record_list_content').html('');
			var param=	{'povertyId':id,'curPage':1};
			U.ajax('/donation/donationPublicList',param,function(json){
				if(json.success){
					var result=json.models.donationList;
					$(result).each(function(){
						$('.record_list_content').append('<div id='+this.id+' class="record_list_detail"><div class="time"><div class="icon"></div>'+ U.getMyDate(this.gmtCreate)+'</div><div class="support">'+this.content+'</div></div>')
					});
				}else{
					M.toast(false, json.errorMsg);
				}
			});
		}
		//捐助
		function donate_list() {
			$('.poor_list tbody').html('');
			var str = {
				'userId': id,
				'state': 2
			};
			var param = {
				'qcommonStr': JSON.stringify(str),
				'curPage': curPage,
				'pageSize': pageSize
			};
			U.ajax('/donation/list', param, function(json) {
				if(json.success) {
					var result = json.models.donationList;
					$(result).each(function() {
						var type = this.goodsType == 1 ? '捐款' : '捐物';
						var status = this.state;
						if(status == 2) {
							status = '完成';
						}
						//$('.poor_list tbody').append('<tr><td>' + this.payer + '</td><td>' + type + '</td><td>' + this.amount / 100 + '</td><td>' + this.content + '</td><td>'+U.getMyDate(this.gmtCreate)+'</td><td>' + status + '</td></tr>');
					});
					//paging_handle(json.models.page, donate_list);
				} else {
					M.toast(false, json.errorMsg);
				}
			});
		}
		function paging_handle(page, method) {
			//分页导航数据处理
			totalPage = page.totalPage;
			$("#paging .page").empty();
			var start_count = 0;
			try {
				if(curPage >= 10) {
					start_count = curPage - 5;
				}
			} catch(e) {}
			var lastsize = totalPage;
			lastsize = start_count + 10;

			if(lastsize >= totalPage) {
				lastsize = totalPage;
			}
			for(var i = start_count; i < lastsize; i++) {
				if(i + 1 == curPage) {
					$("#paging .page").append("<div style='color:white;border-color:white;background-color:#DC3F44'>" + (i + 1) + "</div>");
				} else {
					$("#paging .page").append("<div>" + (i + 1) + "</div>");
				}
			}
			$(".Record>span:nth-child(1)").text("共" + page.totalItem + "条记录");
			$(".Record>span:nth-child(2)").text(curPage + "/" + page.totalPage);
			$("#paging .goto_page>input").val(curPage);
			//分页导航数据处理完毕
			//处理单击事件
			$(".next_page").unbind("click").click(function() {
				if(!page.nextPage) {
					alert("已经是最后一页了!")
					return false;
				}
				curPage++;
				method();
			});
			$(".up_page").unbind("click").click(function() {
				if(curPage == 1) {
					alert("已经是第一页了!");
					return false;
				}
				curPage--;
				method();
			});
			$("#paging .page>div").unbind("click").click(function() {
				curPage = $(this).text();
				method();
			});
			$("#paging .goto").unbind("click").click(function() {
				try {
					var index = $("#paging .goto_page>input").val();
					if(index <= totalPage && index > 0 && !/^[+-]?[1-9]?[0-9]*\.[0-9]*$/.test(index)) {
						curPage = $("#paging .goto_page>input").val();
						method();
					} else {
						alert("请输入正确的页码！");
					}
				} catch(e) {
					alert("请输入正确的页码！");
				}
			});
			//单机事件绑定完毕
		}
	});
};